package com.proyecto.robomind.map;

import com.proyecto.robomind.model.MapSquare;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainMapPanel extends SurfaceView implements SurfaceHolder.Callback{

	private static final String TAG = MainMapPanel.class.getSimpleName();
	
	private MapThread thread;
	private String currentChar;
	private MapSquare[][] currentMap = null;
	private boolean mapLoaded;
	private int height, width, heightCount, widthCount;
	private String avgFps;
	
	public MainMapPanel(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		getHolder().addCallback(this);
		currentChar = "e";
		mapLoaded = false;
		
		thread = new MapThread(getHolder(),this);
		setFocusable(true);
	}
	
	public void createNewMap(int heightCount, int widthCount, int height, int width){
		this.heightCount = heightCount;
		this.widthCount = widthCount;
		this.height = height;
		this.width = width;
		currentMap = new MapSquare[heightCount][widthCount];
		for(int i = 0; i < heightCount; i++){
			for(int c = 0; c < widthCount; c++){
				MapSquare ms = new MapSquare(height/heightCount, width/widthCount, c*(width/widthCount),i*(height/heightCount));
				ms.setCharacter("e");
				currentMap[i][c] = ms;
			}
		}
		mapLoaded = true;
		/*Canvas canvas = getHolder().lockCanvas();
		render(canvas);
		getHolder().unlockCanvasAndPost(canvas);*/
	}
	
	public void loadMap(MapSquare[][] currentMap){
		this.currentMap = currentMap;
		heightCount = currentMap.length;
		this.widthCount = currentMap[0].length;
		mapLoaded = true;
		/*Canvas canvas = getHolder().lockCanvas();
		render(canvas);
		getHolder().unlockCanvasAndPost(canvas);*/
	}
	
	public void setCharacter(String character){
		this.currentChar = character;
	}
	
	public String getCharacter(){
		return currentChar;
	}
	
	public void setMapLoaded(boolean mapLoaded){
		this.mapLoaded = mapLoaded;
	}
	
	public boolean isMapLoaded(){
		return mapLoaded;
	}
	
	public MapSquare[][] getCurrentMap(){
		return currentMap;
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		
	}

	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		thread.setRunning(true);
		thread.start();
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		boolean retry = false;
		while(retry){
			try{
				thread.join();
				retry = true;
			}catch(InterruptedException e){
				
			}
		}	
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		if(event.getAction() == MotionEvent.ACTION_DOWN){
			MapSquare touchedMs = null;
			if(currentMap != null)
				touchedMs = MapHandler.getTouchedMapSquare(event.getX(),event.getY(), currentMap);
			if(touchedMs !=null){
				touchedMs.setCharacter(currentChar);
			}
		}if(event.getAction() == MotionEvent.ACTION_MOVE){
			
		} if(event.getAction() == MotionEvent.ACTION_UP){

		}
		return true;
	}	
	
	public void setAvgFps(String avgFps){
		this.avgFps = avgFps;
	}
	
	protected void render(Canvas canvas){
		canvas.drawColor(Color.WHITE);
		if(currentMap!=null){
			for(int i = 0; i < heightCount; i++){
				for(int c = 0; c < widthCount; c++){
					currentMap[i][c].draw(canvas);
				}
			}
		}
		displayFps(canvas, avgFps);
	}
	
	private void displayFps(Canvas canvas, String fps){
		if(canvas != null && fps!=null){
			Paint paint = new Paint();
			paint.setARGB(255,255,255,255);
			canvas.drawText(fps, this.getWidth()-50,20,paint);
		}
	}
	
	public void update() {
		
	}
	
	public void setThreadRunning(boolean running){
		thread.setRunning(running);
	}
}